﻿package de.gameduell.framework.facebook.pages {
	import de.gameduell.framework.locale.Locale;

	import flash.events.Event;

	import de.gameduell.framework.debug.Debug;
	import de.gameduell.framework.facebook.FBBasePageManager;
	import de.gameduell.framework.facebook.FBDialogEvent;
	import de.gameduell.framework.facebook.Page;
	import de.gameduell.framework.gui.button.Button;
	import de.gameduell.framework.gui.button.ButtonEvent;
	import de.gameduell.framework.gui.button.ButtonGroupAlign;
	import de.gameduell.framework.gui.button.RadioButtonGroup;
	import de.gameduell.framework.gui.button.ToggleButton;

	import flash.display.Sprite;

	public class TabBar extends Page {
		
		private var cashButton:ToggleButton;
		private var inviteButton:ToggleButton;
		private var fanButton:Button;
		private var bookmarkButton:Button;
		private var playButton:ToggleButton;
		private var buttonGroupAlign:ButtonGroupAlign;
		private var locale:Locale;
		
		private var radioButtonGroup:RadioButtonGroup;

		public function TabBar(aPageManager:FBBasePageManager, anId:String) {
			super(aPageManager, anId);
			locale = Locale.getInstance();
			controller.addEventListener(FBDialogEvent.CLOSED, onDialogClosed);
		}
		
		private function onDialogClosed(e:FBDialogEvent):void {
			Debug.trace("TabBar::onDialogClosed: type -> " + e.dialogType);
			radioButtonGroup.reset();
			radioButtonGroup.toggleButton(0); // "play game" button is active again
		}
		
		override protected function initPage():void {
			buttonGroupAlign = new ButtonGroupAlign(-1);
			radioButtonGroup = new RadioButtonGroup();
			
			playButton = new ToggleButton(page.getChildByName("playButton") as Sprite, "playButton");
			playButton.addEventListener(ButtonEvent.CLICK, controller.closeAllFBDialogs);

			cashButton = new ToggleButton(page.getChildByName("cashButton") as Sprite, "cashButton");
			cashButton.addEventListener(ButtonEvent.CLICK, controller.openPaymentPopup);

			inviteButton = new ToggleButton(page.getChildByName("inviteButton") as Sprite, "inviteButton");
			inviteButton.addEventListener(ButtonEvent.CLICK, controller.inviteFriends);
			
			fanButton = new Button(page.getChildByName("fanButton") as Sprite, "fanButton");
			fanButton.addEventListener(ButtonEvent.CLICK, controller.becomeAFan);
			
			bookmarkButton = new Button(page.getChildByName("bookmarkButton") as Sprite, "bookmarkButton");
			bookmarkButton.addEventListener(ButtonEvent.CLICK, controller.bookmarkApp);

			try{
				var iPhoneAppButton:Button = new Button(page.getChildByName("iPhoneAppButton") as Sprite, "iPhoneAppButton");
				iPhoneAppButton.addEventListener(ButtonEvent.CLICK, linkToStore);
			}catch(e:Error){
				// iPhoneButton n/a	
			}
			
			buttonGroupAlign.addButton(playButton);
			buttonGroupAlign.addButton(cashButton);
			buttonGroupAlign.addButton(inviteButton);
			buttonGroupAlign.addButton(fanButton);
			buttonGroupAlign.addButton(bookmarkButton);
			
			radioButtonGroup.addButton(playButton);
			radioButtonGroup.addButton(cashButton);
			radioButtonGroup.addButton(inviteButton);
			
			radioButtonGroup.inActiveAfterPressed = true;
			radioButtonGroup.toggleButton(0);
		}
		
		private function linkToStore(event:Event):void {
			var urlString:String = locale.loadString("iPhoneAppStoreURL");
			controller.redirectTo(urlString);
		}
	}
}
